<template>
  <div class="employees">
    <el-card class="btn">
      <el-button v-print="{ id: 'div' }" type="primary">打印</el-button>
      <el-button
        v-control="'employees_import_button'"
        type="primary"
        @click="goImport"
      >导入</el-button>
      <el-button type="primary" @click="addEvent">+新增员工</el-button>
    </el-card>
    <div id="div">
      <el-card>
        <el-table :data="list">
          <el-table-column label="序号">
            <!-- 具名插槽 scope 当前行的数据 -->
            <template v-slot="scope">
              <div>
                <!-- 当前页码-1 * 每页容量 +当前index+1 -->
                {{ (page.page - 1) * page.size + scope.$index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto">
            <template v-slot="{ row }">
              <div>
                <img
                  v-imagerror="errImg"
                  class="avatar"
                  :src="row.staffPhoto"
                  alt=""
                  @click="imgClick(row.staffPhoto)"
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="{ row }">
              <div>
                {{ row.formOfEmployment | formatType }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry">
            <template v-slot="{ row }">
              <div>
                <!-- 时间过滤器使用 -->
                {{ row.timeOfEntry | formatTime }}
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column label="是否在职" prop="username"/> -->
          <el-table-column label="状态" prop="enableState" />
          <el-table-column label="操作" fixed="right" width="300">
            <template v-slot="{ row }">
              <div>
                <el-button
                  type="text"
                  @click="goDetails(row.id)"
                >查看</el-button>
                <el-button type="text">转正</el-button>
                <el-button type="text">调岗</el-button>
                <el-button type="text">离职</el-button>
                <el-button
                  type="text"
                  @click="roleClick(row.id)"
                >角色</el-button>
                <el-button type="text" @click="delClick(row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div>
          <el-pagination
            :total="page.total"
            :page-size="page.size"
            :current-page="page.page"
            :page-sizes="[1, 5, 10, 100, 200, 300, 400]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
    <!-- 新增 -->
    <Dialog :is-show="show" @update:isShow="show = $event" @getData="getData" />
    <!-- 头像二维码显示 -->
    <el-dialog
      title="二维码"
      :visible.sync="showQrcode"
      style="text-align: center"
    >
      <canvas ref="canvas" />
    </el-dialog>
    <!-- 角色弹框 -->
    <DialogRole ref="dialogRole" />
  </div>
</template>

<script>
import Dialog from './components/dialog.vue'
import { sysUser, sysUserDel } from '@/api/employess.js'
import QrCode from 'qrcode'
import DialogRole from './components/dialogRole.vue'
export default {
  components: { Dialog, DialogRole },
  data() {
    return {
      showQrcode: false, //  头像显示
      errImg: require('@/assets/404_images/404.png'),
      show: false,
      page: {
        page: 1,
        size: 10,
        total: 100
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取数据
    async getData() {
      const res = await sysUser(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log('员工列表', res)
    },
    // 每页容量
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    // 页码
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 新增按钮
    addEvent() {
      this.show = true
    },
    // 删除按钮
    delClick(row) {
      this.$confirm('确定要删除该员工吗?', '提示').then(async() => {
        await sysUserDel(row.id)
        this.$message.success('删除成功')
        this.handleCurrentChange(1)
      })
    },
    // 员工导入
    goImport() {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    // 查看员工信息
    goDetails(id) {
      this.$router.push('/employees/details/' + id)
    },
    // 头像点击
    imgClick(igmsrc) {
      if (!igmsrc) {
        this.$message.error('暂无图片')
        return
      }

      this.showQrcode = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.canvas, igmsrc, {
          width: 300,
          height: 300
        })
      })
    },
    // 角色
    roleClick(id) {
      this.$refs.dialogRole.isShow = true
      this.$refs.dialogRole.getlist(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;
  background-color: #eff1f4;
  .btn {
    display: flex;
    justify-content: right;
  }
  .avatar {
    width: 50px;
    height: 50px;
    object-fit: contain;
  }
}
</style>
